#import "DescriptionSectionCell.h"
#import "Masonry.h"
#define kMaxCollapsedLines 3  // 收起状态下最大显示行数
#define kActionButtonHeight 30 // 操作按钮高度

@interface DescriptionSectionCell ()

@property (nonatomic, strong) UILabel *titleLabel;
//@property (nonatomic, strong) UILabel *descriptionLabel;
@property (nonatomic, strong) UIButton *actionButton;
@property (nonatomic, strong) UIView *separatorLine;

@end

@implementation DescriptionSectionCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    self.backgroundColor = [UIColor whiteColor];
    
    // 创建标题
    _titleLabel = [[UILabel alloc] init];
    _titleLabel.text = @"服务描述";
    _titleLabel.font = [UIFont boldSystemFontOfSize:16.0];
    _titleLabel.textColor = kColorHEX(0x333333);
    [self.contentView addSubview:_titleLabel];
    
    // 创建分割线
    _separatorLine = [[UIView alloc] init];
    _separatorLine.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1.0];
    [self.contentView addSubview:_separatorLine];
    
    // 创建描述文本标签
    _descriptionLabel = [[UILabel alloc] init];
    _descriptionLabel.font = [UIFont systemFontOfSize:14.0];
    _descriptionLabel.textColor = kColorHEX(0x666666);
    _descriptionLabel.numberOfLines = 0;
    [self.contentView addSubview:_descriptionLabel];
    
    // 创建展开/收起按钮
    _actionButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [_actionButton setTitleColor:[UIColor colorWithRed:0.2 green:0.6 blue:0.9 alpha:1.0] forState:UIControlStateNormal];
    _actionButton.titleLabel.font = [UIFont systemFontOfSize:14.0];
    [_actionButton addTarget:self action:@selector(toggleExpand) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_actionButton];
    
    // 设置约束
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(15);
        make.left.equalTo(self.contentView).offset(15);
        make.height.equalTo(@20);
    }];
    
    [_separatorLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_titleLabel.mas_bottom).offset(10);
        make.left.equalTo(self.contentView).offset(15);
        make.right.equalTo(self.contentView).offset(-15);
        make.height.equalTo(@0.5);
    }];
    
    [_descriptionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_separatorLine.mas_bottom).offset(15);
        make.left.equalTo(self.contentView).offset(15);
        make.right.equalTo(self.contentView).offset(-15);
    }];
    
    [_actionButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_descriptionLabel.mas_bottom).offset(10);
        make.left.equalTo(self.contentView).offset(15);
        make.height.equalTo(@kActionButtonHeight);
        make.bottom.equalTo(self.contentView).offset(-15);
    }];
}

- (void)setDescriptionText:(NSString *)descriptionText {
    _descriptionText = descriptionText;
    _descriptionLabel.text = descriptionText;
    
    [self updateUI];
}

- (void)setIsExpanded:(BOOL)isExpanded {
    _isExpanded = isExpanded;
    [self updateUI];
}

- (void)updateUI {
    // 检查文本是否需要折叠
    BOOL needCollapse = [self shouldCollapseText];
    
    // 更新按钮状态
    if (needCollapse) {
        _actionButton.hidden = NO;
        [_actionButton setTitle:_isExpanded ? @"收起" : @"展开" forState:UIControlStateNormal];
        
        // 更新描述标签行数限制
        _descriptionLabel.numberOfLines = _isExpanded ? 0 : kMaxCollapsedLines;
    } else {
        _actionButton.hidden = YES;
        _descriptionLabel.numberOfLines = 0;
    }
    
    [self setNeedsLayout];
}

- (BOOL)shouldCollapseText {
    if (!_descriptionText || _descriptionText.length == 0) {
        return NO;
    }
    
    // 计算完整文本高度
    UILabel *tempLabel = [[UILabel alloc] init];
    tempLabel.font = [UIFont systemFontOfSize:14.0];
    tempLabel.numberOfLines = 0;
    tempLabel.text = _descriptionText;
    
    CGFloat textWidth = self.contentView.bounds.size.width - 30;
    CGSize maxTextSize = CGSizeMake(textWidth, CGFLOAT_MAX);
    [tempLabel sizeToFit];
    CGFloat fullTextHeight = [tempLabel sizeThatFits:maxTextSize].height;
    
    // 计算限制行数后的高度
    CGFloat lineHeight = tempLabel.font.lineHeight;
    CGFloat collapsedHeight = lineHeight * kMaxCollapsedLines;
    
    return fullTextHeight > collapsedHeight;
}

- (void)toggleExpand {
    _isExpanded = !_isExpanded;
    [self updateUI];
    
    // 调用回调通知外部
    if (self.expandCallback) {
        self.expandCallback(_isExpanded);
    }
}

+ (CGFloat)calculateHeightForText:(NSString *)text
                           width:(CGFloat)width
                      isExpanded:(BOOL)isExpanded {
    if (!text || text.length == 0) {
        return 60; // 至少有标题和分割线的高度
    }
    
    // 计算文本高度
    UILabel *tempLabel = [[UILabel alloc] init];
    tempLabel.font = [UIFont systemFontOfSize:14.0];
    tempLabel.numberOfLines = isExpanded ? 0 : kMaxCollapsedLines;
    tempLabel.text = text;
    
    CGFloat textWidth = width - 30;
    CGSize maxTextSize = CGSizeMake(textWidth, CGFLOAT_MAX);
    [tempLabel sizeToFit];
    CGFloat textHeight = [tempLabel sizeThatFits:maxTextSize].height;
    
    // 检查是否需要显示展开/收起按钮
    BOOL needCollapse = [self shouldCollapseForText:text width:width];
    CGFloat actionButtonHeight = needCollapse ? kActionButtonHeight + 20 : 0;
    
    // 总高度 = 标题高度 + 分割线高度 + 文本高度 + 间距 + 按钮高度
    return 55 + textHeight + 15 + actionButtonHeight;
}

+ (BOOL)shouldCollapseForText:(NSString *)text width:(CGFloat)width {
    UILabel *tempLabel = [[UILabel alloc] init];
    tempLabel.font = [UIFont systemFontOfSize:14.0];
    tempLabel.numberOfLines = 0;
    tempLabel.text = text;
    
    CGFloat textWidth = width - 30;
    CGSize maxTextSize = CGSizeMake(textWidth, CGFLOAT_MAX);
    [tempLabel sizeToFit];
    CGFloat fullTextHeight = [tempLabel sizeThatFits:maxTextSize].height;
    
    CGFloat lineHeight = tempLabel.font.lineHeight;
    CGFloat collapsedHeight = lineHeight * kMaxCollapsedLines;
    
    return fullTextHeight > collapsedHeight;
}

@end
